package com.xiaobaibai.server.dashboard;

import com.alibaba.fastjson.JSON;
import com.xiaobaibai.common.response.ResponseFormat;
import com.xiaobaibai.units.Last7DaysUtil;
import com.xiaobaibai.vo.LoveVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.connection.StringRedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DashboardServiceImpl implements IDashboardService{

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Map<String, Integer> getAmountOfData() {
        //需要管道获取
        return null;
    }

    @Override
    public Map<String, Boolean> MapWhetherAdd() {
        return null;
    }

    @Override
    public Map<String, List> Recent7DayArticle() {
        return null;
    }

    @Override
    public Map<String, Object> getDashboardData() {

        List<String> recent7Day = Last7DaysUtil.getRecent7Day();

        //获取各个数量
        //获取是否有新增
        //近7日文章数据  SessionCallback封装好的,优先使用它
        List<Object> list = stringRedisTemplate.executePipelined(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                 redisOperations.opsForValue().get("BlogCount");
                 redisOperations.opsForValue().get("LoveCount");
                 redisOperations.opsForValue().get("CommentCount");
                 redisOperations.opsForValue().get("UserCount");

                redisOperations.hasKey("newUser");
                redisOperations.hasKey("newComment");
                redisOperations.opsForList().range("newLove", 0, -1);
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(6));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(5));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(4));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(3));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(2));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(1));
                redisOperations.opsForValue().get("clickBlog:" + recent7Day.get(0));
                return null;
            }
        });

        System.out.println(JSON.toJSONString(list));
        //各个数量
        Map<String,String> countMap=new HashMap<>();
        countMap.put("BlogCount", (String) list.get(0));
        countMap.put("LoveCount", (String) list.get(1));
        countMap.put("CommentCount", (String) list.get(2));
        countMap.put("UserCount", (String) list.get(3));
        //是否有新增
        Map<String,Boolean> whetherAdd=new HashMap<>();
        whetherAdd.put("haveNewUser", (Boolean) list.get(4));
        whetherAdd.put("haveNewComment", (Boolean) list.get(5));
        //是否有点赞
        List<String> loveList= (List<String>) list.get(6);//文章id-网友地址
        //近7日数据
        //X轴上方已有recent7Day
        List<String> readCount=new ArrayList<>(7);
        for(int i=13;i>=7;i--){
            if( list.get(i)==null)
                readCount.add(String.valueOf(0));
            else
                readCount.add((String) list.get(i));
        }

        Map<String,Object> data=new HashMap<>();

        if(loveList==null||loveList.size()<1) {
//            whetherAdd.put("haveNewLove", false);//改进:根据loveList的长度来判断
            data.put("loveList",loveList);
        }
        else {
            List<LoveVo> loveVos=new ArrayList<>();
//            whetherAdd.put("haveNewLove", true);
            loveList.forEach(l->{
                String[] split = l.split("-");
                LoveVo loveVo=new LoveVo(split[0],split[1]);
                loveVos.add(loveVo);
            });
            data.put("loveList",loveVos);
        }

        data.putAll(countMap);
        data.putAll(whetherAdd);
        data.put("readCount",readCount);
        data.put("recent7day",recent7Day);

        return data;
    }

    @Override
    public void receiveLove(String str) {

        List<String> list=JSON.parseArray(str,String.class);
        List<String> deleteList=new ArrayList<>(list.size());
        list.forEach(l->{
            LoveVo loveVo = JSON.parseObject(l, LoveVo.class);
            deleteList.add(loveVo.blogId+"-"+loveVo.address);
        });

        stringRedisTemplate.executePipelined(new SessionCallback() {
            @Override
            public Object execute(RedisOperations redisOperations) throws DataAccessException {
                deleteList.forEach(l->{
                    //删除newLove这个List[key]中的一个值为l的元素,阿拉伯数字1代表从左往右找一个,-1从右往左,0删除所有
                    redisOperations.opsForList().remove("newLove",1,l);
                });
                return null;
            }
        });

    }


}
